package com.java.system.controller;

import com.java.system.entity.Area;
import com.java.system.entity.Camera;
import com.java.system.entity.Result;
import com.java.system.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  区域控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    IAreaService areaService;
    @GetMapping("/areaList")
    public Result getArea(){
        List<Area> areaList = areaService.getArea();
        if(areaList!=null){
            Result result=Result.success("获取区域列表成功");
            result.put("data",areaList);
            return result;
        }
        else {
            return Result.error("获取信息失败");
        }
    }
    @GetMapping("/cameraList/{id}")
    public Result getCameraListById(@PathVariable("id")Integer id){
        List<Camera> cameraList = areaService.getCameraListById(id);
        int size = cameraList.size();
        if(!StringUtils.isEmpty(cameraList)){
            Result result=Result.success("获取摄像头列表成功");
            result.put("data",cameraList);
            result.put("size",size);
            return result;
        }
        else {
            return Result.error("获取信息失败");
        }

    }
    @PostMapping("/add")
    public Result insertArea(@RequestBody Area area){
        areaService.insertArea(area);
        return Result.success("增加区域成功");

    }
    @GetMapping("/{id}")
    public Result getAreaById(@PathVariable("id")Integer id){
        Area area = areaService.getAreaById(id);
        if(area!=null){
            Result result=Result.success("获取区域成功");
            result.put("data",area);
            return result;
        }
        else {
            return Result.error("获取信息失败");
        }

    }
    @PutMapping
    public Result updateArea(@RequestBody Area area){
        areaService.updateArea(area);
        return Result.success("更新区域信息成功");

    }
    @DeleteMapping("/{id}")
    public Result deleteArea(@PathVariable("id")Integer id){
        areaService.deleteArea(id);
        return Result.success("删除区域信息成功");
    }
}
